//
//  LoginVM.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/5/21.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import UIKit
import RxSwift

struct LoginVM {
    
    static let minUsernameLength = 11
    static let maxUsernameLength = 11
    
    
    static let minPasswordLength = 6
    static let maxPasswordLength = 12
    
    // 输出
    let usernameValid: Observable<Bool>

    let passwordValid: Observable<Bool>

    let everythingValid: Observable<Bool>

    init(username: Observable<String>,
         password: Observable<String>) {
        
        usernameValid = username.map{
            $0.count >= LoginVM.minUsernameLength && $0.count <= LoginVM.maxUsernameLength
        }.share(replay: 1)
        
        passwordValid = password.map {
            $0.count >= LoginVM.minPasswordLength && $0.count <= LoginVM.maxPasswordLength
        }.share(replay: 1)
        
        everythingValid = Observable.combineLatest(usernameValid, passwordValid) { $0 && $1 }.share(replay: 1)
        
    }
    
    func sumbit() {
        
    }
    
}
